//package service.pic
//
//import com.jdpc.weparty.component.ServerConfig
//import com.jdpc.weparty.service.activity.ActivityService
//import com.jdpc.weparty.service.activity.generateActivityInfo
//import com.jdpc.weparty.service.user.UserService
//import com.jdpc.weparty.service.user.generateUserInfo
//import org.springframework.beans.factory.annotation.Autowired
//import org.springframework.stereotype.Service
//import org.springframework.web.multipart.MultipartFile
//import java.awt.image.BufferedImage
//import java.io.File
//import java.lang.Exception
//import javax.imageio.ImageIO
//import javax.servlet.http.HttpServletResponse
//
//@Service
//class PictureServiceImpl : PictureService {
//    @Autowired
//    private lateinit var serverConfig: ServerConfig
//
//    @Autowired
//    private lateinit var activityService: ActivityService
//
//    @Autowired
//    private lateinit var userService: UserService
//
//    private val supportedFileType = listOf("PNG", "JPG", "JPEG")
//
//    private fun readPic(parent: String, picName: String): BufferedImage {
//        val file = File(parent, picName)
//        if (!file.exists()) {
//            throw Exception("图片不存在")
//        }
//        return ImageIO.read(file)
//    }
//
//    private fun responsePic(pic: BufferedImage, picName: String, response: HttpServletResponse) {
//        var picType = picName.split(".")[1]
//        if (picType == "jpg") {
//            picType = "jpeg"
//        }
//        response.contentType = "image/$picType"
//        val os = response.outputStream
//        try {
//            ImageIO.write(pic, picType, os)
//
//        } catch (e: Exception) {
//            e.printStackTrace()
//        } finally {
//            os.flush()
//            os.close()
//        }
//    }
//
//    override fun responseAvatar(response: HttpServletResponse, picName: String) {
//        val pic = readPic(serverConfig.localPath + serverConfig.avatarPath, picName)
//        responsePic(pic, picName, response)
//    }
//
//    override fun responseBanner(response: HttpServletResponse, picName: String) {
//        val pic = readPic(serverConfig.localPath + serverConfig.bannerPath, picName)
//        responsePic(pic, picName, response)
//    }
//
//    override fun saveAvatar(file: MultipartFile, userId: Int): String? {
//        var type: String? = null
//        file.originalFilename?.let {
//            type = getFileType(it)
//        } ?: return null
//        if (!file.isEmpty) {
//            type?.let {
//                if (it.toUpperCase() in supportedFileType) { // 项目在容器中实际发布运行的根路径
//
//                    val trueFileName = "user$userId.${it.toLowerCase()}"
//                    // 设置存放图片文件的路径
//                    val path = "${serverConfig.localPath}${serverConfig.avatarPath}/$trueFileName"
//                    val f = File(path)
//                    if (f.exists()) {
//                        f.delete()
//                    }
//                    file.transferTo(File(path))
//                    val url = "avatar/$trueFileName"
//                    val user = userService.getUser(userId)
//                    user?.let { u ->
//                        u.avatar = url
//
//                    } ?: return null
//                    userService.changeInfo(userId, generateUserInfo(user))
//
//                    return url
//                }
//            }
//        }
//        return null
//
//    }
//
//    override fun saveBanner(file: MultipartFile, aId: Int): String? {
//        var type: String? = null
//        file.originalFilename?.let {
//            type = getFileType(it)
//        } ?: return null
//        if (!file.isEmpty) {
//            type?.let {
//                if (it.toUpperCase() in supportedFileType) { // 项目在容器中实际发布运行的根路径
//                    val info = activityService.findActivity(aId) ?: return null
//                    val trueFileName = "activity$aId.$it"
//                    // 设置存放图片文件的路径
//                    val path = "${serverConfig.localPath}${serverConfig.bannerPath}/$trueFileName"
//                    file.transferTo(File(path))
//                    //修改数据库内的活动图片路径
//                    info.activityPic = "banner/$trueFileName"
//                    activityService.changeActivityInfo(aId, generateActivityInfo(info))
//
//                    return info.activityPic
//                }
//            }
//        }
//        return null
//
//    }
//
//    private fun getFileType(fileName: String): String? {
//        return if (fileName.indexOf(".") != -1) fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length) else null
//
//    }
//}